<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎页</title>
    <link rel="stylesheet" href="../../static/frame/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/frame/static/css/style.css">
    <link rel="icon" href="../../static/frame/static/image/code.png">
</head>
<body class="body">

<div class="layui-row layui-col-space10 my-index-main">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">选择应用</label>
            <div class="layui-input-block">
                <table class="layui-hide" id="applist" lay-filter="applist"></table>
                <div class="layui-form-mid layui-word-aux">注意：切换页面会自动取消当前选择的应用<br>
                </div>
                <div class="layui-form-mid layui-word-aux" id="nowchecked"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">级别名称</label>
            <div class="layui-input-block">
                <input type="text" name="grade_name" required lay-verify="required" placeholder="代理级别的名称"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拿货折率</label>
            <div class="layui-input-block">
                <input type="text" name="grade_present" required lay-verify="required" placeholder="填写0.1则为1折"
                       autocomplete="off" class="layui-input">
                <div class="layui-form-mid layui-word-aux">【请输入小于1的小数】代理实际支付 = 基础价格 *
                    折率；例如：套餐价格为100元，代理折率为0.9，则代理购买价格为90元
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开通价格</label>
            <div class="layui-input-block">
                <input type="text" name="grade_price" required lay-verify="required" placeholder="开通此级别的价格"
                       autocomplete="off" class="layui-input">
                <div class="layui-form-mid layui-word-aux">本级别的价格(用于在线开通代理)，为0或空则不开启自助开通/升级</div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">添加级别</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>


    <table class="layui-hide" id="gradelist" lay-filter="gradelist"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="app">应用设置</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>

</div>


<script src="../../static/jquery-3.3.1.js"></script>
<script type="text/javascript" src="../../static/frame/layui/layui.js"></script>
<script type="text/javascript" src="../../static/js/index.js"></script>
<script type="text/javascript" src="../../static/frame/echarts/echarts.min.js"></script>
<script type="text/javascript">


    layui.use(['form', 'table', 'element', 'layer', 'vip_tab'], function () {
        // 打开选项卡
        $('.my-nav-btn').on('click', function () {
            if ($(this).attr('data-href')) {
                vipTab.add($(this), '<i class="layui-icon">' + $(this).find("button").html() + '</i>' + $(this).find('p:last-child').html(), $(this).attr('data-href'));
            }
        });
        var table = layui.table, form = layui.form;
        var table2 = layui.table;
        window.table3 = layui.table;
        var load = layer.load();
        $.ajax({
            url: '../ajax.php?mod=user_applist',
            type: 'POST',
            dataType: 'html',
            data: '',
            success: function (data) {
                layer.close(load);
                $('#appid').html(data);
                form.render('select');
                //layer.msg(data);

            },
            error: function (data) {
                layer.close(load);
                layer.msg('请求失败' + data);
            }
        });
        table2.on('checkbox', function (obj) {
            var checkStatus = table2.checkStatus('applist'), appdata = checkStatus.data;
            var str = '';
            for (var i in appdata) { //遍历object
                if (str === '') {
                    str = appdata[i].id
                } else {
                    str += ',' + appdata[i].id
                }
            }
            $('#nowchecked').html('当前选择的应用ID列表：' + str);
        });


        table2.render({
            elem: '#applist'
            , url: '../ajax.php?mod=getapplist'
            , cellMinWidth: 130
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 60}
                , {field: 'app_name', title: '应用名称'}
            ]]
            , page: true
        });


        table.render({
            elem: '#gradelist'
            , url: '../ajax.php?mod=getgradelist'
            , cellMinWidth: 130
            , cols: [[
                {field: 'ID', title: 'ID', width: 60}
                , {field: 'appname', title: '所属应用'}
                , {field: 'lname', title: '级别名称', edit: 'text'}
                , {field: 'fracture', title: '拿货折率', edit: 'text'}
                , {field: 'price', title: '级别购价', edit: 'text'}
                , {toolbar: '#barDemo', title: '操作', width: 240}
            ]]
            , page: true
        });


        table.on('tool', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么，删除后此等级下的代理将会一并删除！', function (index) {
                    $.ajax({
                        url: '../ajax.php?mod=delgrade',
                        type: 'POST',
                        dataType: 'html',
                        data: 'gid=' + data.ID,
                        success: function (data) {
                            //layer.close(loading);
                            obj.del();
                            layer.msg(data);
                        },
                        error: function (data) {
                            //layer.close(loading);
                            layer.msg('请求失败' + data);
                        }
                    });
                    layer.close(index);
                });
            }else if (obj.event === 'app'){
                layer.confirm('<table class="layui-hide" id="applist2" lay-filter="applist2"></table>\n' +
                    '                <div class="layui-form-mid layui-word-aux">注意：切换页面会自动取消当前选择的应用<br>\n' +
                    '                </div>\n' +
                    '                <div class="layui-form-mid layui-word-aux" id="nowchecked2"></div>\n', {
                    btn: ['确定','取消'],
                    anim: 1,
                    area: ['490px', '670px'],
                    title:'请选择应用'
                }, function(){
                    var checkStatus = table3.checkStatus('applist2'), appdata = checkStatus.data;
                    var str = '';
                    for (var i in appdata) { //遍历object
                        if (str === '') {
                            str = appdata[i].id
                        } else {
                            str += ',' + appdata[i].id
                        }
                    }
                    if (str === null || str === ''){
                        layer.alert('未选择应用，自动取消');
                        return;
                    }
                    $.ajax({
                        url: '../ajax.php?mod=changegrade',
                        type: 'POST',
                        dataType: 'html',
                        data: 'gid=' + data.ID + '&mod=appid&value=' + str,
                        success: function (data) {
                            layer.msg(data);
                        },
                        error: function (data) {
                            layer.msg('请求失败' + data);
                        }
                    });

                    table.reload('gradelist');
                });
                table3_r();
            }
        });

        table.on('edit(gradelist)', function (obj) {
            $.ajax({
                url: '../ajax.php?mod=changegrade',
                type: 'POST',
                dataType: 'html',
                data: 'gid=' + obj.data.ID + '&mod=' + obj.field + '&value=' + obj.value,
                success: function (data) {
                    layer.msg(data);
                },
                error: function (data) {
                    layer.msg('请求失败' + data);
                }
            })
        });

        form.on('submit', function (data) {
            var loading = layer.load();
            var checkStatus = table2.checkStatus('applist'), appdata = checkStatus.data;
            var str = '';
            for (var i in appdata) { //遍历object
                if (str === '') {
                    str = appdata[i].id
                } else {
                    str += ',' + appdata[i].id
                }
            }

            $.ajax({
                url: '../ajax.php?mod=addgrade',
                type: 'POST',
                dataType: 'html',
                data: 'grade_name=' + data.field.grade_name + '&grade_present=' + data.field.grade_present + '&grade_price=' + data.field.grade_price + '&app=' + str,
                success: function (data) {
                    layer.close(loading);
                    layer.msg(data);
                    table.reload('gradelist', {url: '../ajax.php?mod=getgradelist'});
                },
                error: function (data) {
                    layer.close(loading);
                    layer.msg('请求失败' + data);
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    });
    function table3_r() {
        table3.render({
            elem: '#applist2'
            , url: '../ajax.php?mod=getapplist'
            , cellMinWidth: 130
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 60}
                , {field: 'app_name', title: '应用名称'}
            ]]
            , page: false
            ,limit:999
        });

        table3.on('checkbox', function (obj) {
            var checkStatus = table3.checkStatus('applist2'), appdata = checkStatus.data;
            var str = '';
            for (var i in appdata) { //遍历object
                if (str === '') {
                    str = appdata[i].id
                } else {
                    str += ',' + appdata[i].id
                }
            }
            $('#nowchecked2').html('当前选择的应用ID列表：' + str);
        });
    }

</script>
</body>
</html>